package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDishAndFlavor(DishDTO dishDTO) {

        //向菜品表中插入数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.addDish(dish);

        //向菜品风味表中插入数据
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null &&flavors.size()>0){
            //将每一个风味绑定到菜品上
            Long dishId = dish.getId();
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            //直接把flavors集合传过去，批量插入数据
            dishFlavorMapper.addDishFlavor(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void deleteDish(List<Long> ids) {
        //判断菜品是否是起售，起售则不能删除
        for (Long id : ids) {
            DishVO dish = dishMapper.queryDishById(id);
            if (dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断菜品是否关联了套餐
        List<Long> setmealIds = setmealDishMapper.querySetmealIdByDishId(ids);
        if (setmealIds != null && setmealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
        dishMapper.deleteDish(ids);
        //删除菜品描述
        dishFlavorMapper.deleteDishFlavor(ids);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO queryDishById(Long id) {
        //查询菜品
        DishVO dishVO = dishMapper.queryDishById(id);
        //查询菜品风味
        List<DishFlavor> dishFlavorList = dishFlavorMapper.queryDishFlavorById(id);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Transactional
    @Override
    public void updateDishAndFlavor(DishDTO dishDTO) {
        //修改菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);

        //修改风味
        //删除原有的风味数据
        Long dishDTOId = dishDTO.getId();
        List<Long> dishFlavorId= new ArrayList<>();
        dishFlavorId.add(dishDTOId);
        dishFlavorMapper.deleteDishFlavor(dishFlavorId);
        //添加新的风味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()){
            for (DishFlavor flavor : flavors) {
                //将风味绑在菜品上
                flavor.setDishId(dishDTOId);
            }
            dishFlavorMapper.addDishFlavor(flavors);
         }

    }

    /**
     * 修改菜品售卖状态
     */
    public void updateStatus(Integer status, Long id) {
        Dish dish = Dish.builder().status(status).id(id).build();
        dishMapper.updateDish(dish);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> queryDishByCategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.queryDishByCategoryId(categoryId);
        return dishList;
    }


    /**
     * 客户端根据分类id和售卖状态查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.queryDishByCategoryIdWithStatus(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.queryDishFlavorById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
